package com.example.demo4;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    /**
     * 用于ListView显示的数据
     */
    private String[] data = { "AppLe", "Banana" ,"Orange", "Watermelon",
            "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango",
            "Apple","Banana", "Orange", "Wate rmelon", "Pear" ,
            "Grape","Pineapple", "Strawberry", "Cherry", "Mango" };
    private List<Fruit> fruitList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //隐藏原标题栏
        ActionBar actionBar=getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }
        //初始化水果数据
        initFruits();
//        //创建水果类的适配器
//        FruitAdapter fruitAdapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList);
//        //注册ListView
//        ListView listView=findViewById(R.id.listView);
//        //添加适配器
//        listView.setAdapter(fruitAdapter);
//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Fruit fruit=fruitList.get(position);
//                Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
//            }
//        });

        //注册布局
        RecyclerView recyclerView=findViewById(R.id.recycler);

        //创建一个线性布局管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        //指定线性布局方向
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        //创建网格布局管理器
        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3);
        //指定布局方向
        gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);

        //创建交错网格布局管理器,指定栏数和布局方向
        StaggeredGridLayoutManager manager=new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL);

        //添加布局管理器
        recyclerView.setLayoutManager(manager);
        //创建适配器
        FruitsAdapter fruitsAdapter=new FruitsAdapter(fruitList);
        //添加适配器
        recyclerView.setAdapter(fruitsAdapter);
    }

    /**
     * 初始化水果数据
     */
    private void initFruits(){
        for (String datum : data) {
            Fruit fruit=new Fruit(datum,R.mipmap.fruit,R.id.fruit_name);
            fruitList.add(fruit);
        }
    }
}
